package com.product.android.commonInterface.contact;

import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONObject;

import android.text.TextUtils;

import com.common.android.utils.JSONUtils;
import com.nd.android.u.allcommon.R;
import com.product.android.business.ApplicationVariable;
import com.product.android.commonInterface.allCommonInterfaceImpl.AllCommonCallOtherModel;
import com.product.android.commonInterface.chat.ChatGroup;

public class OapGroup implements Serializable {
	private static final long serialVersionUID = 1L;
	public static final int CONFIG_GROUPPERMISSION_DEFAULT = 2;             //默认群设置
	public static final  int  MASKGROUPMSG=0;          //不接收
	public static final  int  RECEIVESHOW=2;           //接收不提醒
	public static final  int  RECEIVENOTIFY=1;         //接收并提醒
	public static final  int  SETGROUPPARA=3;  //改变群消息的参数
	
	public static final int INITGROUPSETTYPE =-1;
	public static final int DEPARTGROUP = 1;
	public static final int CLASSGROUP = 2;
	public static final int NORMALGROUP =0;
	public static final int RECENTGROUP = -1;
	public static final int DISCUSSIONGROUP =3;
	public static final int CLUBGROUP =4;
	public static final int PERSON = -99; //个人
	public int mgroupSetType=INITGROUPSETTYPE;

	public static final int CATEGORY_DEPART_GROUP = 4;//部门群
	public static final int CATEGORY_STUDENT_TEACHER_GROUP = 5; //老师学生群
	public static final int CATEGORY_PARENT_TEACHER_GROUP = 6; //老师家长群
	public static final int CATEGORY_CLUB_GROUP = 7; //老师家长群
	
	public int type;
	private int _id;
	private long uid;
	private long gid;
	private String groupName = "";
	private int category;	//部门群-4，老师学生群-5，老师家长群-6
	private int membernum;
	private int level;
	private String introduction = "";		//群组简介
	private String notice = "";
	private int joinperm;				//加入权限
	private int avatar;					//图标编号
	private String imageUrl = "";
	private long creatorid;				//创建者的ID
	private OapGroupRelation manager=null;
	private String updatetime = "";
	private int groupType;
	private int oapDeptId;
//	private OapDepart depart;
	private int classid;					// 班级id
//	private int classType;					// 班级类型 老师--1 学生--2 家长--3
	private int club;
	private int falg;                     //标识 －1 为不可用
	private ArrayList<OapGroupRelation> groupMemberList = new ArrayList<OapGroupRelation>();
	
	public OapGroup(){}
	
	public OapGroup(JSONObject json){
		initGroup(json);
	}
	
	//普通群
	public void initGroup(JSONObject json){
		this.gid = JSONUtils.getInt(json, "gid");
		this.groupName = JSONUtils.getString(json, "gname");
		this.category = JSONUtils.getInt(json, "category");
		this.avatar = JSONUtils.getInt(json, "avatar");
		this.level = JSONUtils.getInt(json, "level");
		this.introduction = JSONUtils.getString(json, "introduction");
		this.uid= ApplicationVariable.INSTANCE.getIUser().getBindUser().getUid();
		this.membernum = JSONUtils.getInt(json, "membernum");
		this.notice = JSONUtils.getString(json, "notice");
		this.updatetime = JSONUtils.getString(json, "updatetime");
		this.creatorid = JSONUtils.getLong(json, "creatorid");
		this.joinperm = JSONUtils.getInt(json, "joinperm");
		this.club = JSONUtils.getInt(json, "club");
		this.groupType = NORMALGROUP;
		this.falg=0;
	}
	
	
	public void copyByOtherGroup(OapGroup group){
		if(group!=null){
			this.uid = group.getUid();
			this.gid = group.getGid();
			this.groupName = group.getGroupName();
			this.category = group.getCategory();
			this.avatar = group.getAvatar();
			this.level = group.getLevel();
			this.introduction = group.getIntroduction();
			this.membernum = group.getMembernum();
			this.notice = group.getNotice();
			this.updatetime = group.getUpdatetime();
			this.creatorid = group.getCreatorid();
			this.joinperm=group.getJoinperm();
			this.falg=0;
		}
	}
	
	public void initDiscussionGroup(JSONObject json){
		if(json!=null){
			this.uid = ApplicationVariable.INSTANCE.getIUser().getBindUser().getUid();
			this.groupName = JSONUtils.getString(json, "gname");
			this.gid = JSONUtils.getLong(json, "gid");
			this.notice = JSONUtils.getString(json,"notice");
			this.groupType = DISCUSSIONGROUP;
			this.falg=0;
		}
	}
	
	public int getDeptId() {
		return oapDeptId;
	}

	public void setOapDeptId(int oapDeptId) {
		this.oapDeptId = oapDeptId;
	}

	public int get_id() {
		return _id;
	}
	
	public void set_id(int id) {
		_id = id;
	}
	
	public long getUid() {
		return uid;
	}
	
	public void setUid(long uid) {
		this.uid = uid;
	}	

	public String getGroupName() {
		if(!TextUtils.isEmpty(groupName)) return groupName;

		groupName = AllCommonCallOtherModel.ContactModel.getGroupName(this);
//		switch(groupType){
//		case NORMALGROUP:
//			break;
//		case DEPARTGROUP:
//			if(depart==null){
//				depart = DaoFactory.getInstance().getOapDepartDao().searchDepartByDeptid(GlobalVariable.getInstance().getUid(), oapDeptId);
//			}
//			groupName = depart.getDeptname();
//			break;
//		case CLASSGROUP:
//			OapClass  oapClass = DaoFactory.getInstance().getOapClassDao().searchOapClasss(GlobalVariable.getInstance().getUid(),classid);
//			if(oapClass!=null){
//				switch(classType){
//				case 0:
//					groupName = oapClass.getClassname();
//					break;
//				case 1:
//					groupName = oapClass.getClassname()+OapApplication.getInstance().getResources().getString(R.string.teacher1);
//					break;
//				case 2:
//					groupName = oapClass.getClassname()+OapApplication.getInstance().getResources().getString(R.string.student);
//					break;
//				case 3:
//					groupName = oapClass.getClassname()+OapApplication.getInstance().getResources().getString(R.string.parents);
//					break;
//				}
//				
//			}
//			else{
//				groupName="";
//			}
//			break;
//		}
		return groupName;
	}

//	public boolean isManager(long fid){
//		if(creatorid==fid) return true;
//		if(groupMemberList!=null){
//			for(OapGroupRelation gm:groupMemberList){
//				if(gm.getFid()==fid&&(gm.getGrade()==2||gm.getGrade()==3)){
//					return true;
//				}
//			}
//		}
//		return false;
//	}
	
	public boolean isCreater(long fid){
		if(creatorid==fid) return true;
		else return false;
	}
	
	public void setGroupname(String groupname) {
		this.groupName = groupname;
	}
	
	public String getIntroduction() {
		return introduction;
	}
	
	public long getGid() {
		return gid;
	}

	public void setGid(long gid) {
		this.gid = gid;
	}

	public void setIntroduction(String introduction) {
		this.introduction = introduction;
	}
	
	public int getAvatar() {
		return avatar;
	}
	
	public void setAvatar(int avatar) {
		this.avatar = avatar;
	}
	
	public String getImageUrl() {
		return imageUrl;
	}
	
	public void setImageUrl(String imageUrl) {
		this.imageUrl = imageUrl;
	}
	
	String groupKey="";
	public String getGroupKey(){
		if(groupType==NORMALGROUP||groupType==DISCUSSIONGROUP){
			return gid+"";
		}
		else if(groupType==DEPARTGROUP){
			return gid+"";
//			return String.format("%s-d-%s-u",oapDeptId,ApplicationVariable.INSTANCE.getCurrentUser().getUnitid());
		}
		else if(groupType==CLASSGROUP){
			return gid+"";
//			if(groupKey!=null&&"".equals(groupKey)){
//				switch(classType){
//				case 0:
//					groupKey =String.format("%s-c-%s-s",classid,ApplicationVariable.INSTANCE.getCurrentUser().getUnitid());
//					break;
//				case 1:
//				case 2:
//				case 3:
//					groupKey =  String.format("%s-mt-%s-c-%s-s",classType,classid,ApplicationVariable.INSTANCE.getCurrentUser().getUnitid());
//					break;
//				}
//			}
//			return groupKey;
		}
		return groupKey;
	}
	
	public int getChatGroupType(){
		if(groupType==NORMALGROUP){
			return ChatGroup.getNormalGroupType();
		}
		else if(groupType==DISCUSSIONGROUP){
			return ChatGroup.getDiscussionGroupType();
		}
		else{
			return ChatGroup.getDepartGroupType();
		}
	}

	@Override
	public boolean equals(Object o) {
		if(o instanceof OapGroup){
			OapGroup group = (OapGroup)o;
			if(group.getUid()==this.getUid()
					&&group.getGid()==this.getGid()
					&&group.getGroupType()==this.groupType
					&&group.getDeptId()==this.oapDeptId
					&&group.getClassid()==this.classid){
				return true;
			}
		}
		return false;
	}
	
	public void setGMemberList(ArrayList<OapGroupRelation> groupMemberList) {
		this.groupMemberList = groupMemberList;
	}
//	public void setGroupMemberList(List<OapGroupRelation> groupMemberList) {
//		this.groupMemberList = groupMemberList;
//	}

	//获取群成员
	public ArrayList<OapGroupRelation> getGMemberList() {
		return groupMemberList;
	}
//	public List<OapGroupRelation> getGroupMemberList() {
//		if(groupMemberList!=null) {
//			return groupMemberList;
//		}		
//		//dyf temp
////		switch(groupType){
////		case DEPARTGROUP:
////			if(groupMemberList!=null){
////				groupMemberList.clear();
////			}
////			else{
////				groupMemberList = new ArrayList<OapGroupRelation>();
////			}
////			DepartGroupPro.getInstance().getDepartGroupMember(oapDeptId, groupMemberList);
////			break;
////		case CLASSGROUP:
////			if(groupMemberList!=null){
////				groupMemberList.clear();
////			}
////			else{
////				groupMemberList = new ArrayList<OapGroupRelation>();
////			}
////			DepartGroupPro.getInstance().getClassGroupMember(classid, classType,groupMemberList);
////			break;
////		case DISCUSSIONGROUP:
////		case NORMALGROUP:
////			groupMemberList = DaoFactory.getInstance().getOapGroupRelationDao().
////					searchGroupRelations(ApplicationVariable.INSTANCE.getCurrentUser().getUid(), gid, 0);
////			break;
////	
////		}
////		if(groupMemberList==null){
////			groupMemberList = new ArrayList<OapGroupRelation>();
////		}
//		return groupMemberList;
//	}

	@Override
	public int hashCode() {
		return (int) gid;
	}

	public int getCategory() {
		return category;
	}

	public int getMembernum() {
		return membernum;
	}

	public int getClassid() {
		return classid;
	}

//	public int getClassType() {
//		return classType;
//	}

	public void setClassid(int classid) {
		this.classid = classid;
	}

//	public void setClassType(int classType) {
//		this.classType = classType;
//	}

	public int getLevel() {
		return level;
	}

	public String getNotice() {
		return notice;
	}

	public OapGroupRelation getManager() {
		return manager;
	}

	public String getUpdatetime() {
		return updatetime;
	}

	public int getGroupType() {
		return groupType;
	}

	
	/**
	 * 因为本地群类型定义与服务端定义的群类型是不一样的，所以当我们提供给聊天模块使用的时候
	 * 需要把本地的群类型定义转换成服务端的群类型定义
	 * 
	 * 服务端的群类型定义参见聊天模块MessageConst.GroupType
	 * 
	 * <br>Created 2014-10-27 下午2:43:52
	 * @return
	 * @author       yanghq
	 */
	public int getGroupServerType(){
		switch (groupType) {
		case DEPARTGROUP:
		case CLASSGROUP:
			return 10;  // DEPARTMENT=10;CLASS=10;

		case NORMALGROUP:
			return 0;   // NORMAL=0;		
			
		case RECENTGROUP:
			return -1;   //服务端没有对应数据，原样返回
			
		case DISCUSSIONGROUP: 
			return 2;   //DISCUSSION=2;
			
		case CLUBGROUP:
			return 4;    //服务端没有对应类型，原样返回
		
		default:
			return 0;
		}
	}
	
	public void setGroupType(int groupType) {
		this.groupType = groupType;
	}

	public void setCategory(int category) {
		this.category = category;
	}

	public void setMembernum(int membernum) {
		this.membernum = membernum;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public void setNotice(String notice) {
		this.notice = notice;
	}

	public void setManager(OapGroupRelation manager) {
		this.manager = manager;
	}

	public void setUpdatetime(String updatetime) {
		this.updatetime = updatetime;
	}

	public long getCreatorid() {
		return creatorid;
	}

	public void setCreatorid(long creatorid) {
		this.creatorid = creatorid;
	}
	
//	public String getGroupKeyPre(){
//		return GlobalVariable.getInstance().getUid()+"-"+getGroupKey();
//	}

	public int getJoinperm() {
		return joinperm;
	}

	public void setJoinperm(int joinperm) {
		this.joinperm = joinperm;
	}

	public String getJoinpermStr() {
		if(joinperm==0){                       //需要验证才能加入该群
			return ApplicationVariable.INSTANCE.applicationContext.getString(R.string.need_validation_to_join_the_group);
		}
		else if(joinperm==1){                //允许任何人加入该群
			return ApplicationVariable.INSTANCE.applicationContext.getString(R.string.allowed_to_join_group);
		}
		else{                              //不允许任何人加入该群
			return ApplicationVariable.INSTANCE.applicationContext.getString(R.string.do_not_allow_anyone_to_join_the_group);
		}
	}
	
	public int getJoinpermPostion(){
		if(joinperm==0){ return 1;			
		}
		else if(joinperm==1) return 0;
		return 2;
	}
	
	public int getClub() {
		return club;
	}

	public void setClub(int club) {
		this.club = club;
	}

	public int getFalg() {
		return falg;
	}

	public void setFalg(int falg) {
		this.falg = falg;
	}
}